package com.whut.report;

import com.alibaba.fastjson.JSONArray;
import com.whut.common.ReportCommonFunc;
import com.whut.model.Profiles;
import com.whut.model.UrlProperties;
import com.whut.parMapper.ParProblemCollectDetailMapper;
import com.whut.parMapper.ParProblemDescriptionMapper;
import com.whut.parMapper.ParSystemMapper;
import com.whut.parMapper.utils.MapperUtils;
import com.whut.service.impl.ReportServiceImpl;
import com.whut.utils.MDoc;
import com.whut.utils.StringOperate;
import com.whut.utils.pointsCalculate;
import com.whut.v_model.CheckAndProject;
import com.whut.v_model.ProblemCollectDetail;
import com.whut.v_model.TimesStat;
import org.apache.commons.beanutils.BeanMap;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;

import javax.annotation.Resource;
import java.awt.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.whut.common.ReportCommonFunc.generateReport;

public class
SecurityReport {
    @Resource
    private UrlProperties urlProperties;
    private ParSystemMapper parSystemMapper;
    private ParProblemDescriptionMapper parProblemDescriptionMapper;
    private ParProblemCollectDetailMapper parProblemCollectDetailMapper;
    private ReportServiceImpl reportServiceImpl;
    private Profiles profiles;
    private MapperUtils mapperUtils;

    private int checkId;
    private int projectId;
    private boolean isNew;
    public static final int WIDTH = 718;
    public static final int HEIGHT = 468;
    public SecurityReport(ParSystemMapper parSystemMapper,
                                           ParProblemDescriptionMapper parProblemDescriptionMapper,
                                           ParProblemCollectDetailMapper parProblemCollectDetailMapper,
                                           ReportServiceImpl reportServiceImpl,
                                           MapperUtils mapperUtils,
                                           UrlProperties urlProperties,
                                           Profiles profiles
    ) {
        this.parSystemMapper = parSystemMapper;
        this.parProblemDescriptionMapper = parProblemDescriptionMapper;
        this.parProblemCollectDetailMapper = parProblemCollectDetailMapper;
        this.reportServiceImpl = reportServiceImpl;
        this.mapperUtils=mapperUtils;
        this.urlProperties=urlProperties;
        this.urlProperties=urlProperties;
        this.profiles=profiles;

    }

    public String getSecurityReport(String client, String check, String checkInfo, boolean isNew) throws UnsupportedEncodingException {
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
        ChartFactory.setChartTheme(standardChartTheme);
        //应用主题样式
        DecimalFormat df = new DecimalFormat("0.00");
        Map<String, Object> dataMap = new HashMap<>();
        Date date = new Date();
        DateFormat d1 = new SimpleDateFormat("yyyyMMddhhmmss");
        DateFormat d2 = new SimpleDateFormat("yyyy");
        DateFormat d3 = new SimpleDateFormat("MM");
        String month = d3.format(date);
        String year = d2.format(date);
        String fileName = d1.format(date);
        dataMap.put("month", month);
        dataMap.put("year", year);
        JSONArray jsonArray = JSONArray.parseArray(checkInfo);
        JSONArray jsonArray1 = JSONArray.parseArray(client);
        int contractId = jsonArray1.getIntValue(0);
        dataMap.put("client", mapperUtils.getContractinfoMapper().getClientByContractId1(contractId).getClient());
        JSONArray jsonArray2 = JSONArray.parseArray(check);
        java.util.List<Integer> list2 = new ArrayList<>();
        for (int j = 0; j < jsonArray2.size(); j++) {
            list2.add(jsonArray2.getIntValue(j));
        }
        java.util.List<CheckAndProject> rankPointList = mapperUtils.getContractTaskProjectMapper().getCheckAndPointOrderByPoint(list2);
        dataMap.put("projectNum", jsonArray2.size());
        java.util.List<Integer> allCheckItemsIdList = parProblemCollectDetailMapper.getAllFourthIdByCheckList(list2, 1);
        java.util.List<String> allCheckItemsNameInfoList = new ArrayList<>();
        for (int i = 0; i < allCheckItemsIdList.size(); i++) {
            allCheckItemsNameInfoList.add(parSystemMapper.getName4ByName4Id(allCheckItemsIdList.get(i)));
        }
        String allCheckItems = StringOperate.listToString(allCheckItemsNameInfoList, "、");
        dataMap.put("allCheckItems", allCheckItems);
        //一、检查项目排名部分
        java.util.List<String> unitTypeList = new ArrayList<>();
        unitTypeList.add("sgUnit");
        unitTypeList.add("jlUnit");
        unitTypeList.add("jsUnit");
        java.util.List<Object> unitRankList = new ArrayList<>();
        for (int i = 0; i < unitTypeList.size(); i++) {
            Map<String, Object> unitRankMap = new HashMap<>();
            java.util.List<Object> unitRankInnerList = new ArrayList<>();
            switch (unitTypeList.get(i)) {
                case "sgUnit":
                    unitRankMap.put("unitType", "施工单位");
                    break;
                case "jlUnit":
                    unitRankMap.put("unitType", "监理单位");
                    break;
                case "jsUnit":
                    unitRankMap.put("unitType", "建设单位");
                    break;
                default:
            }
            for (int j = 0; j < rankPointList.size(); j++) {
                Map<String, Object> unitRankInnerMap = new HashMap<>();
                unitRankInnerMap.put("rank", j + 1);
                unitRankInnerMap.put("projectName", rankPointList.get(j).getProjectName());
                switch (unitTypeList.get(i)) {
                    case "sgUnit":
                        unitRankInnerMap.put("unit", rankPointList.get(j).getSgUnit());
                        break;
                    case "jlUnit":
                        unitRankInnerMap.put("unit", rankPointList.get(j).getJlUnit());
                        break;
                    case "jsUnit":
                        unitRankInnerMap.put("unit", rankPointList.get(j).getJsUnit());
                        break;
                    default:
                }
                unitRankInnerMap.put("point", rankPointList.get(j).getTotalPoint() == null ? "0" : df.format(Double.parseDouble(rankPointList.get(j).getTotalPoint())));
                unitRankInnerMap.put("riskPoint", rankPointList.get(j).getLatestRiskLevel() == null ? "" : rankPointList.get(j).getLatestRiskLevel());
                if (rankPointList.get(j).getTotalPoint() == null) {
                    unitRankInnerMap.put("overAllMerit", "不合格");
                } else {
                    if (Double.parseDouble(rankPointList.get(j).getTotalPoint()) >= 70) {
                        unitRankInnerMap.put("overAllMerit", "合格");
                    } else {
                        unitRankInnerMap.put("overAllMerit", "不合格");
                    }
                }
                unitRankInnerList.add(unitRankInnerMap);
            }
            unitRankMap.put("unitRankInnerList", unitRankInnerList);
            unitRankList.add(unitRankMap);
        }
        dataMap.put("unitRankList", unitRankList);

        //二、安全隐患分布情况(一)隐患等级分布比例
        TimesStat timesStat = parProblemCollectDetailMapper.getAllSecurityRiskPoint(list2, 1);
        int allProblemNum = parProblemCollectDetailMapper.getAllSecurityProblemNum(list2, 1);
        dataMap.put("level4", timesStat.getCount4());
        dataMap.put("level3", timesStat.getCount3());
        dataMap.put("level2", timesStat.getCount2());
        dataMap.put("level01", timesStat.getCount0() + timesStat.getCount1());
        dataMap.put("allProblemNum", allProblemNum);
        dataMap.put("level4Percent", pointsCalculate.caculateToPercent(timesStat.getCount4(), allProblemNum));
        dataMap.put("level3Percent", pointsCalculate.caculateToPercent(timesStat.getCount3(), allProblemNum));
        dataMap.put("level2Percent", pointsCalculate.caculateToPercent(timesStat.getCount2(), allProblemNum));
        dataMap.put("level01Percent", pointsCalculate.caculateToPercent(timesStat.getCount0() + timesStat.getCount1(), allProblemNum));
        DefaultPieDataset datasetRiskPie = new DefaultPieDataset();
        datasetRiskPie.setValue("提醒和特征值1" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount0() + timesStat.getCount1(), allProblemNum), new Double(timesStat.getCount0() + timesStat.getCount1()));
        datasetRiskPie.setValue("特征值2" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount2(), allProblemNum), new Double(timesStat.getCount2()));
        datasetRiskPie.setValue("特征值3" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount3(), allProblemNum), new Double(timesStat.getCount3()));
        datasetRiskPie.setValue("特征值4" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount4(), allProblemNum), new Double(timesStat.getCount4()));
        try {
            JFreeChart chart = ChartFactory.createPieChart("隐患分布饼图", datasetRiskPie, true, true, false);
            final PiePlot plot = (PiePlot) chart.getPlot();
            plot.setStartAngle(0);
            plot.setForegroundAlpha(0.90f);
            plot.setInteriorGap(0.02);
            chart.getLegend().setVisible(false);
            plot.setSectionPaint("提醒和特征值1" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount0() + timesStat.getCount1(), allProblemNum), new Color(49, 95, 232));
            plot.setSectionPaint("特征值2" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount2(), allProblemNum), new Color(232, 232, 49));
            plot.setSectionPaint("特征值3" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount3(), allProblemNum), new Color(240, 117, 60));
            plot.setSectionPaint("特征值4" + ":" + pointsCalculate.caculateToPercent(timesStat.getCount4(), allProblemNum), new Color(232, 36, 18));
            File pieChart = new File("D:\\whutServer\\generatedCharts\\" + fileName + "securityRiskPieChart.png");
//            File pieChart = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "securityRiskPieChart.png");
            ChartUtils.saveChartAsPNG(pieChart, chart, 718, 468);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        dataMap.put("pic1", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "securityRiskPieChart.png"));
        dataMap.put("pic1", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\" + fileName + "securityRiskPieChart.png"));
        //（二）安全隐患分布比例
        java.util.List<Object> problemNumList = new ArrayList<>();
        DefaultPieDataset datasetPie = new DefaultPieDataset();
        DefaultCategoryDataset datasetBar = new DefaultCategoryDataset();
        final String Bar = "各检查分项";
        for (int i = 0; i < allCheckItemsIdList.size(); i++) {
            Map<String, Object> problemNumMap = new HashMap<>();
            problemNumMap.put("no", i + 1);
            problemNumMap.put("pName4", parSystemMapper.getName4ByName4Id(allCheckItemsIdList.get(i)));
            int innerProblemNum = parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i));
            problemNumMap.put("problemNum", innerProblemNum);
            problemNumMap.put("percent", pointsCalculate.caculateToPercent(innerProblemNum, allProblemNum));
            datasetPie.setValue(parSystemMapper.getName4ByName4Id(allCheckItemsIdList.get(i)) + ":" + pointsCalculate.caculateToPercent(innerProblemNum, allProblemNum), new Double(innerProblemNum));
            datasetBar.addValue(innerProblemNum, Bar, parSystemMapper.getName4ByName4Id(allCheckItemsIdList.get(i)));
            problemNumList.add(problemNumMap);
        }
        dataMap.put("problemNumList", problemNumList);
        try {
            JFreeChart chart = ChartFactory.createPieChart("现场施工问题分布饼图", datasetPie, true, true, false);
            final PiePlot plot = (PiePlot) chart.getPlot();
            plot.setStartAngle(270);
            plot.setForegroundAlpha(0.90f);
            plot.setInteriorGap(0.02);
            chart.getLegend().setVisible(false);
            File pieChart = new File("D:\\whutServer\\generatedCharts\\" + fileName + "securityPieChart.png");
//            File pieChart = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "securityPieChart.png");
            ChartUtils.saveChartAsPNG(pieChart, chart, 718, 468);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            JFreeChart barChart = ChartFactory.createBarChart("现场施工问题分布直方图",
                    "",
                    "数量",
                    datasetBar,
                    PlotOrientation.VERTICAL,
                    true, true, false);
            CategoryPlot plot = (CategoryPlot) barChart.getPlot();
            plot.getRenderer().setSeriesPaint(0, new Color(5, 87, 232));
            BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
            barRenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
            barRenderer.setDefaultItemLabelsVisible(true);
            barRenderer.setItemLabelAnchorOffset(-20D);
            barRenderer.setMaximumBarWidth(0.2);
            CategoryAxis categoryAxis = plot.getDomainAxis();
            categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
            File barChartFile = new File("D:\\whutServer\\generatedCharts\\" + fileName + "securityBarChart.png");
//            File barChartFile = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "securityBarChart.png");
            ChartUtils.saveChartAsPNG(barChartFile, barChart, 718, 468);
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataMap.put("pic2", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\" + fileName + "securityPieChart.png"));
        dataMap.put("pic3", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\" + fileName + "securityBarChart.png"));
//        dataMap.put("pic2", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "securityPieChart.png"));
//        dataMap.put("pic3", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName + "securityBarChart.png"));
        //三、安全隐患汇总分析
        java.util.List<Object> part3List = new ArrayList<>();
        for (int i = 0; i < allCheckItemsIdList.size(); i++) {
            java.util.List<Object> part3InnerList = new ArrayList<>();
            Map<String, Object> part3Map = new HashMap<>();
            part3Map.put("pName4", parSystemMapper.getName4ByName4Id(allCheckItemsIdList.get(i)));
            java.util.List<Integer> part3ProjectNumList = parProblemCollectDetailMapper.getSecurityPart3ProjectNum(list2, allCheckItemsIdList.get(i));
            int level4Num = 0;
            int level3Num = 0;
            int level2Num = 0;
            int level01Num = 0;
            part3Map.put("projectNum", part3ProjectNumList.size());
            for (int y = 0; y < part3ProjectNumList.size(); y++) {
                java.util.List<ProblemCollectDetail> part3RiskProjectNumList = parProblemCollectDetailMapper.getAllProblemCollectInfoByCheckId(part3ProjectNumList.get(y));
                String maxRisk = pointsCalculate.getMaxRiskPoint(part3RiskProjectNumList);
                switch (maxRisk) {
                    case "提醒":
                        level01Num++;
                        break;
                    case "1":
                        level01Num++;
                        break;
                    case "2":
                        level2Num++;
                        break;
                    case "3":
                        level3Num++;
                        break;
                    case "4":
                        level4Num++;
                        break;
                    default:
                }
            }
            part3Map.put("level4Num", level4Num);
            part3Map.put("level3Num", level3Num);
            part3Map.put("level2Num", level2Num);
            part3Map.put("level01Num", level01Num);
            part3Map.put("problemNum", parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i)));
            TimesStat timesStat1 = parProblemCollectDetailMapper.getSecurityRiskPoint(list2, allCheckItemsIdList.get(i));
            part3Map.put("level4", timesStat1.getCount4());
            part3Map.put("level3", timesStat1.getCount3());
            part3Map.put("level2", timesStat1.getCount2());
            part3Map.put("level01", timesStat1.getCount0() + timesStat1.getCount1());
            part3Map.put("level4Percent", pointsCalculate.caculateToPercent(timesStat1.getCount4(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))));
            part3Map.put("level3Percent", pointsCalculate.caculateToPercent(timesStat1.getCount3(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))));
            part3Map.put("level2Percent", pointsCalculate.caculateToPercent(timesStat1.getCount2(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))));
            part3Map.put("level01Percent", pointsCalculate.caculateToPercent(timesStat1.getCount0() + timesStat1.getCount1(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))));
            DefaultPieDataset datasetInnerRiskPie = new DefaultPieDataset();
            datasetInnerRiskPie.setValue("提醒和特征值1" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount0() + timesStat1.getCount1(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Double(timesStat1.getCount0() + timesStat1.getCount1()));
            datasetInnerRiskPie.setValue("特征值2" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount2(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Double(timesStat1.getCount2()));
            datasetInnerRiskPie.setValue("特征值3" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount3(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Double(timesStat1.getCount3()));
            datasetInnerRiskPie.setValue("特征值4" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount4(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Double(timesStat1.getCount4()));
            try {
                JFreeChart chart = ChartFactory.createPieChart(parSystemMapper.getName4ByName4Id(allCheckItemsIdList.get(i)) + "隐患分布饼图", datasetInnerRiskPie, true, true, false);
                final PiePlot plot = (PiePlot) chart.getPlot();
                plot.setStartAngle(0);
                plot.setForegroundAlpha(0.90f);
                plot.setInteriorGap(0.02);
                chart.getLegend().setVisible(false);
                plot.setSectionPaint("提醒和特征值1" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount0() + timesStat1.getCount1(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Color(49, 95, 232));
                plot.setSectionPaint("特征值2" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount2(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Color(232, 232, 49));
                plot.setSectionPaint("特征值3" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount3(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Color(240, 117, 60));
                plot.setSectionPaint("特征值4" + ":" + pointsCalculate.caculateToPercent(timesStat1.getCount4(), parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Color(232, 36, 18));
//                File pieChart = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityInnerRiskPieChart.png");
                File pieChart = new File("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityInnerRiskPieChart.png");
                ChartUtils.saveChartAsPNG(pieChart, chart, 718, 468);
            } catch (Exception e) {
                e.printStackTrace();
            }
            part3Map.put("pic1", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityInnerRiskPieChart.png"));
//            part3Map.put("pic1",MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityInnerRiskPieChart.png"));

            DefaultCategoryDataset datasetRiskBar = new DefaultCategoryDataset();
            final String flag1 = "提醒和特征值1";
            final String flag2 = "特征值2";
            final String flag3 = "特征值3";
            final String flag4 = "特征值4";
            final String riskBar = "特征值情况统计";
            datasetRiskBar.addValue(timesStat1.getCount0() + timesStat1.getCount1(), flag1, riskBar);
            datasetRiskBar.addValue(timesStat1.getCount2(), flag2, riskBar);
            datasetRiskBar.addValue(timesStat1.getCount3(), flag3, riskBar);
            datasetRiskBar.addValue(timesStat1.getCount4(), flag4, riskBar);
            try {
                JFreeChart barChart = ChartFactory.createBarChart("隐患分布直方图",
                        "",
                        "数量",
                        datasetRiskBar,
                        PlotOrientation.VERTICAL,
                        true, true, false);
                CategoryPlot plot = (CategoryPlot) barChart.getPlot();
                plot.getRenderer().setSeriesPaint(0, new Color(49, 95, 232));
                plot.getRenderer().setSeriesPaint(1, new Color(232, 232, 49));
                plot.getRenderer().setSeriesPaint(2, new Color(240, 117, 60));
                plot.getRenderer().setSeriesPaint(3, new Color(232, 36, 18));
                BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
                barRenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
                barRenderer.setDefaultItemLabelsVisible(true);
                barRenderer.setItemLabelAnchorOffset(-20D);
                File barChartFile = new File("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityRiskBarChart.png");
//                File barChartFile = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityRiskBarChart.png");

                ChartUtils.saveChartAsPNG(barChartFile, barChart, 718, 468);
            } catch (Exception e) {
                e.printStackTrace();
            }
            part3Map.put("pic2", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityRiskBarChart.png"));
//            part3Map.put("pic2",MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityRiskBarChart.png"));

            java.util.List<Integer> part3InnerSixthInfoList = parProblemCollectDetailMapper.getSecurityInnerSixthId(list2, allCheckItemsIdList.get(i));
            java.util.List<String> part3InnerSixthNameList = new ArrayList<>();
            for (int k = 0; k < part3InnerSixthInfoList.size(); k++) {
                part3InnerSixthNameList.add(parSystemMapper.getName4ByName4Id(part3InnerSixthInfoList.get(k)));
            }
            part3Map.put("allpName6", StringOperate.listToString(part3InnerSixthNameList, "、"));
            DefaultPieDataset datasetInnerPie = new DefaultPieDataset();
            DefaultCategoryDataset datasetInnerBar = new DefaultCategoryDataset();
            for (int j = 0; j < part3InnerSixthInfoList.size(); j++) {
                Map<String, Object> part3InnerMap = new HashMap<>();
                part3InnerMap.put("no", j + 1);
                part3InnerMap.put("pName6", parSystemMapper.getName4ByName4Id(part3InnerSixthInfoList.get(j)));
                int part3InnerProblemNum = parProblemCollectDetailMapper.getPart3InnerProblemNum(list2, part3InnerSixthInfoList.get(j));
                part3InnerMap.put("problemNum", part3InnerProblemNum);
                part3InnerMap.put("percent", pointsCalculate.caculateToPercent(part3InnerProblemNum, parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))));
                java.util.List<Integer> part3InnerProjectList = parProblemCollectDetailMapper.getPart3InnerProject(list2, part3InnerSixthInfoList.get(j));
                part3InnerMap.put("projectNum", part3InnerProjectList.size());
                datasetInnerPie.setValue(parSystemMapper.getName4ByName4Id(part3InnerSixthInfoList.get(j)) + pointsCalculate.caculateToPercent(part3InnerProblemNum, parProblemCollectDetailMapper.getSecurityProblemNum(list2, allCheckItemsIdList.get(i))), new Double(part3InnerProblemNum));
                datasetInnerBar.addValue(part3InnerProblemNum, Bar, parSystemMapper.getName4ByName4Id(part3InnerSixthInfoList.get(j)));
                java.util.List<ProblemCollectDetail> part3InnerDescriptionList = parProblemCollectDetailMapper.getPart3InnerDescriptionAllInfo(list2, part3InnerSixthInfoList.get(j));
                List<String> part3InnerDescriptionAllList = new ArrayList<>();
                for (int m = 0; m < part3InnerDescriptionList.size(); m++) {
                    if (part3InnerDescriptionList.get(m).getIsRemark().equals("N")) {
                        part3InnerDescriptionAllList.add(part3InnerDescriptionList.get(m).getProblemDescription());
                    } else if (part3InnerDescriptionList.get(m).getIsRemark().equals("Y")) {
                        part3InnerDescriptionAllList.add(part3InnerDescriptionList.get(m).getRemark());
                    }
                }
                part3InnerMap.put("allProblemDescription", StringOperate.listToString(part3InnerDescriptionAllList, "、"));
               /* List<Object> picList = new ArrayList<>();
                for(int u=0;u<part3InnerDescriptionList.size();u++)
                {
                    String picPath=part3InnerDescriptionList.get(u).getPicPath();
                    String picName=part3InnerDescriptionList.get(u).getPicName();
                    JSONArray ja = JSONArray.parseArray(picName);
                    if (ja.size() == 0) {
                        //String s = MDoc.getImageStr("C:\\Users\\96500\\Desktop\\20190329183836002.png");
                        String s = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                        picList.add(s);
                    } else {
                        for (int o = 0; o < ja.size(); o++) {
                            String s = MDoc.getImageStr(picPath + ja.getString(o));
                            //String s = MDoc.getImageStr("C:\\Users\\96500\\Desktop\\20190329183836002.png");
                            picList.add(s);
                        }
                    }
                }
                part3InnerMap.put("picList",picList);*/
                part3InnerList.add(part3InnerMap);
            }
            try {
                JFreeChart chart = ChartFactory.createPieChart("现场施工问题分布饼图", datasetInnerPie, true, true, false);
                final PiePlot plot = (PiePlot) chart.getPlot();
                plot.setStartAngle(270);
                plot.setForegroundAlpha(0.90f);
                plot.setInteriorGap(0.02);
                chart.getLegend().setVisible(false);
                File pieChart = new File("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityPieInnerChart.png");
//                File pieChart = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityPieInnerChart.png");

                ChartUtils.saveChartAsPNG(pieChart, chart, 718, 468);
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                JFreeChart barChart = ChartFactory.createBarChart("现场施工问题分布直方图",
                        "",
                        "数量",
                        datasetInnerBar,
                        PlotOrientation.VERTICAL,
                        true, true, false);
                CategoryPlot plot = (CategoryPlot) barChart.getPlot();
                plot.getRenderer().setSeriesPaint(0, new Color(5, 87, 232));
                BarRenderer barRenderer = (BarRenderer) plot.getRenderer();
                barRenderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
                barRenderer.setDefaultItemLabelsVisible(true);
                barRenderer.setItemLabelAnchorOffset(-20D);
                barRenderer.setMaximumBarWidth(0.2);
                CategoryAxis categoryAxis = plot.getDomainAxis();
                categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
                File barChartFile = new File("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityBarInnerChart.png");
//                File barChartFile = new File("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityBarInnerChart.png");

                ChartUtils.saveChartAsPNG(barChartFile, barChart, 718, 468);
            } catch (Exception e) {
                e.printStackTrace();
            }
            part3Map.put("pic3", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityPieInnerChart.png"));
            part3Map.put("pic4", MDoc.getImageStr("D:\\whutServer\\generatedCharts\\" + fileName + i + "securityBarInnerChart.png"));
//            part3Map.put("pic3", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityPieInnerChart.png"));
//            part3Map.put("pic4", MDoc.getImageStr("E:\\tencent_File\\1296928795\\FileRecv\\yirui\\" + fileName +i+ "securityBarInnerChart.png"));

            part3Map.put("part3InnerList", part3InnerList);
            part3List.add(part3Map);
        }
        dataMap.put("part3List", part3List);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMddhhmmss");
        String fileNameTime = dateTimeFormatter.format(LocalDateTime.now());
        String reportName = "安全总报" + fileNameTime;
        String ftlName = "securityReport2.0";


        return ReportCommonFunc.generateReport(dataMap, reportName, ftlName);
    }
}
